Skip to content

Add the ash_workflow.gen.acknowledgement recipe - #139

Merged
C-Sinclair merged 18 commits into
mainfrom
feat/recipe-acknowledgement
Nov 6, 2026
Merged

C-Sinclair merged 18 commits into
mainfrom
feat/recipe-acknowledgement

Conversation

@C-Sinclair

@C-Sinclair C-Sinclair commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor
  • Adds mix ash_workflow.gen.acknowledgement, a request recipe with one row per person per subject. It generates <Subject>Acknowledgement with an identity :unique_person on (subject_id, person_id). The subject gains has_many :acknowledgements and an acknowledged_by_actor? calculation. Options: --person (required), --overdue-after, --module, --domain, --transition-log.
  • Recipe.create_policy/2 gates the :request create action on a generated Checks.CanRequest, which denies everyone until the application fills it in.
  • Recipe.belongs_to/4 types subject_id and person_id after the primary keys of the subject and the --person resource.
  • Recipe.validate_interval/2 rejects an --overdue-after shorter than the default Oban scheduler's precision floor with an Igniter issue.
  • Adds the design in documentation/design/acknowledgement-recipe.md and the recipe page, and links the gen.acknowledgement row in the recipes index. CHANGELOG.md lists the task under Unreleased.
  • This branch carries the commits of Log no transition when a create upserts onto an existing row #147 until that PR merges. Log no transition when a create upserts onto an existing row #147 fixes AshWorkflow.Changes.RecordEvent so an upsert onto an existing row logs no :initial transition and skips AshWorkflow.Scheduler.notify_state_change/1, and raises the ash requirement to ~> 3.29. The :request upsert relies on both.
stateDiagram-v2
  [*] --> pending
  pending --> acknowledged: acknowledge
  pending --> overdue: timeout overdue_after
  overdue --> acknowledged: acknowledge
Loading

Opening requests:

PolicyAcknowledgement.request(policy.id, user.id, actor: manager)
PolicyAcknowledgement.request_many(policy.id, Enum.map(staff, &%{person_id: &1.id}), actor: manager)
PolicyAcknowledgement.acknowledge(acknowledgement, actor: user)

Decisions

  • acknowledge is authorised by relates_to_actor_via(:person). The recipe generates no check module for it, because the person asked is the person who confirms in every application.
  • :request upserts on :unique_person with upsert_fields []. Asking a person who already has a row returns that row unchanged, so request_many/3 can be called with the whole list each time it grows. A test asserts that repeated request_many/3 calls log one :initial row per person.
  • request_many/3 is a second code interface on :request with args: [:subject_id]. A list of maps runs Ash.bulk_create/4. A generic action would have needed the subject's primary key type for its subject_id argument.
  • The subject calculation is acknowledged_by_actor?, reading ^actor(:id) and ending in || false, so it returns false, not nil, for an actor with no acknowledgement. A version taking a person_id argument would need a do block, which Ash.Resource.Igniter.defines_calculation/3 does not match on a second run.
  • The subject has no has_one :latest_acknowledgement. recipes.md gives one to every request recipe, but here the latest row belongs to whichever person was asked last.
  • The overdue timeout reads requested_at, so a transition an application adds that loops back into :pending does not move the deadline.

No change to Mix.AshWorkflow.Recipe. config/config.exs gains one ash_domains line for the new fixture domain, which Ash.Domain.Igniter.add_resource_reference/3 needs in order to find it.

@C-Sinclair
C-Sinclair force-pushed the feat/recipe-expiry branch 2 times, most recently from 2087214 to 9d4aad6 Compare October 22, 2026 12:14
@C-Sinclair
C-Sinclair force-pushed the feat/recipe-acknowledgement branch from 51d6018 to 1144974 Compare October 22, 2026 12:44
… row

`AshWorkflow.Changes.RecordEvent` no longer calls `AshWorkflow.Scheduler.notify_state_change/1` for a create whose record carries `upsert_action: :update` metadata. The row stays in the step it was in, so its deadlines are unchanged.

The `[:ash_workflow, :transition]` span still emits `:stop` on that path. Its `:start` event fires in `change/3`, before the data layer reports whether the row was inserted, and skipping `:stop` would leave the span open.
A create action with `upsert_condition` returns the existing row with `upsert_skipped: true` metadata when the condition does not match and `return_skipped_upsert?` is set. That row carries no `upsert_action` metadata, so `AshWorkflow.Changes.RecordEvent` appended an `:initial` log row and called `AshWorkflow.Scheduler.notify_state_change/1` for it. Both are now skipped, the same as for an upsert that updated the row.
`AshWorkflow.Changes.RecordEvent` reads `upsert_action` metadata to tell an upsert that inserted a row from one that updated it. Ash 3.29.0 added that metadata to `Ash.DataLayer.Ets` and `Ash.DataLayer.Mnesia`. With an older Ash, every upsert on ETS still logs an `:initial` row.

AshPostgres sets the metadata from 2.10.0 on PostgreSQL 17 and later, and from 2.13.1 on every PostgreSQL version. AshPostgres is not a runtime dependency, so `mix.exs` cannot raise that floor for users.
`AshWorkflowTest.Postgres.UpsertWorkflow` upserts on a unique `code` and logs to `AshWorkflowTest.Postgres.UpsertTransition`. Its tests check that a single upsert and a bulk upsert onto an existing row write no second `:initial` row. On ETS, `AshWorkflow.UpsertTest` checks that a bulk upsert of one existing and one new code logs only the new row and does not notify the scheduler for the existing one.
@C-Sinclair

Copy link
Copy Markdown
Contributor Author

Findings from generating CapturePolicyAcknowledgement in Jamzoom with mix ash_workflow.gen.acknowledgement Jamzoom.Tenancy.CapturePolicy --person Jamzoom.Tenancy.Member. The subject and the person both use attribute multitenancy on organisation_id and the AshEvents.Events extension. The generator ran from this branch, and the app now runs on main at 7f62036.

1. The generated resource ignores the subject's multitenancy and data-layer extensions

The acknowledgement resource and its transition log had no multitenancy block. They also lacked the subject's AshEvents.Events extension and any references for the subject and person foreign keys. Without on_delete, deleting a capture policy or a member fails on the foreign key. The code compiles, so nothing points at the gap.

Suggestion: copy the subject's multitenancy strategy and attribute, and its data-layer extensions, onto the generated resource. Emit references with on_delete: :delete for subject and person.

2. The transition log cannot be tenant-scoped

RecordEvent and TransitionLog.history/2 read and write the log with no tenant, so a tenant-scoped log fails every transition. This is not specific to this recipe, so it is filed as #162. With --no-transition-log the recipe works: it generates the acknowledgement resource and Checks.CanRequest only.

3. The generated policies forbid every read

The recipe adds Ash.Policy.Authorizer with policies for :request and :acknowledge only. No policy applies to :read, so every authorized read is forbidden. That includes the subject's acknowledged_by_actor? calculation, whose exists(acknowledgements, ...) filter is then always false.

Suggestion: generate a read policy, such as relates_to_actor_via(:person), or say in the generator notice that reads are denied until the app adds one.

4. The first policy needs a SAT solver the recipe does not add

In an app with no policies yet, compiling the generated resource raised No SAT solver available, because neither picosat_elixir nor simple_sat was a dependency.

Suggestion: add a SAT solver dependency through Igniter when the app has none, or name the requirement in the notice.

5. The recipe depends on #147, which no release contains

:request upserts on unique_person with upsert_fields []. Against hex 0.11.0, a repeated request logs a second :initial transition, and three Jamzoom tests failed. #147 is on main but not in a release.

Suggestion: release #147 before or with this recipe.

6. A repeated request still records an AshEvents event

With AshEvents, a :request that hits an existing row is recorded as another create event, although the upsert changed nothing. The event log then shows one :request per time a member was asked, not one per acknowledgement. This may be acceptable. A line in the recipe docs would save the next reader the investigation.

7. RecordEvent runs again during AshEvents replay

This is not specific to this recipe either. AshWorkflow.Transformers.AddActions adds RecordEvent to the resource-level changes on create. AshEvents.Events.Transformers.WrapActions wraps only action-level changes in ReplayChangeWrapper. On replay, RecordEvent sets state_entered_at to the replay time and its after_action runs again. That hook appends a duplicate log row and calls AshWorkflow.Scheduler.notify_state_change/2. Jamzoom works around this with a transformer that wraps resource-level changes for replay.

Suggestion: attach RecordEvent to each create action instead of the resource-level changes, so AshEvents wraps it like any other action change.

# Conflicts:
#	CHANGELOG.md
#	lib/ash_workflow/changes/record_event.ex
#	mix.exs
#	test/mix/ash_workflow/recipe_test.exs
#	test/support/postgres/domain.ex
@C-Sinclair
C-Sinclair marked this pull request as ready for review November 6, 2026 09:47
@C-Sinclair
C-Sinclair changed the base branch from feat/recipe-expiry to main November 6, 2026 11:19
# Conflicts:
#	CHANGELOG.md
#	documentation/recipes/index.md
#	mix.exs
#	test/mix/ash_workflow/recipe_test.exs
@C-Sinclair
C-Sinclair merged commit 830d980 into main Nov 6, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant